package com.itheima.Task;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;
@Component
@Slf4j
public class CleanOssPictureTask {

        @Autowired
        private RedisTemplate redisTemplate;

        @Autowired
        private AliOssUtil aliOssUtil;

        @Scheduled(cron = "0 0/3 * * * ? ")
        public void clearOrderOSSPictures(){
            log.info("清理过期OSS图片开始-----------------------------------");
            Set allMembers = redisTemplate.opsForSet().members(RedisConstant.SETMEAL_PIC_RESOURCES);
            Set dbMembers = redisTemplate.opsForSet().members(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            for (Object allMember : allMembers) {
                if (!dbMembers.contains(allMember)){
                    redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_RESOURCES,allMember);
                    aliOssUtil.delete(allMember.toString());
                    log.info("清理过期OSS图片"+allMember);
                }
            }
            log.info("清理过期OSS图片结束-----------------------------------");
        }

}
